#include "MonoString.h"
#include <ctype.h>
#include <iostream>

using namespace Mono;

#define EXSTR  for (string::iterator itor = inStr.begin(); itor != inStr.end(); itor++)

void STR::pureWChar(string& inStr)
{
    if (inStr.size() <= 1) { inStr = ""; }
		else
		{
			EXSTR
			{
			if (isascii(inStr.at(0))){ inStr.erase(0, 1); } if (isascii(*itor))
			{
				inStr.erase(itor, itor + 1);if(itor!=inStr.begin()) itor--;
			}	
		}
	}
}

string STR::strip(string inStr, string toStrip)
{
	for (string::iterator itor = toStrip.begin(); itor != toStrip.end(); itor++)
	{
		strip(inStr,(char)*itor);
	}
	return inStr;
}

string STR::strip(string inStr, char toStrip)
{
	EXSTR{if(inStr.at(0) == toStrip){ inStr.erase(0, 1); }
	if (*itor == toStrip){ inStr.erase(itor, itor + 1);if(itor != inStr.begin())itor--; }}
	return inStr;
}

void STR::cstrip(string& inStr, string toStrip)
{
	string::iterator itor = toStrip.begin();
	for (; itor != toStrip.end(); itor++) { cstrip(inStr, (char)*itor);}
}

void STR::cstrip(string& inStr, char toStrip)
{
	EXSTR
	{ 
		if (inStr.at(0) == toStrip){ inStr.erase(0, 1); }
		if (*itor == toStrip){ inStr.erase(itor, itor + 1); if (itor != inStr.begin()) itor--;}
	}
}

int STR::findStr(string& inStr, string toFind)
{
	if ((inStr.length() == 0) || (toFind.length() == 0) || (toFind.length()>inStr.length())){return -1;}
	else
	{
		string::iterator itor2 = toFind.begin();
		EXSTR{
			while (*itor == *itor2) 
			{	itor2++; if (itor2 == toFind.end()){ return itor - inStr.begin(); } }
			continue;
		}
	}
	return -1;
}

void STR::addcslashes(string& inStr)
{
	EXSTR
	{ 
		if ((*itor == '\'')||(*itor == '\"')||(*itor == '\\'))
		{
			if (itor != inStr.begin())
			{
				if (*itor-- == '\\'){ itor++; continue; }
			}
			if (itor != inStr.end())
			{
				if (*itor++ == '\\'){ continue; }
			}
			inStr.insert(itor, '\\');
		}
	}
}

void STR::addslashes(string& inStr,char toAdd)
{
	EXSTR
	{ if (*itor == toAdd) 
	{ if (itor != inStr.begin()) {if (*itor-- == '\\'){ itor++; continue; }}
	  if (itor != inStr.end()){ if (*itor++ == '\\'){ continue; }}
			inStr.insert(itor, '\\');} }
}

void STR::trim(string& inStr)
{
	cstrip(inStr, ' ');
}

void STR::wordwrap(string& inStr,int inWidth,char ofBreak,bool isCut)
{
	if (inWidth <= 1){ inWidth = DEFAULT_WORDWRAP_WIDTH; } inWidth++;
	unsigned int WordCount = inWidth;
	for (; WordCount <= inStr.length(); WordCount++)
	{
		if (WordCount % inWidth == 0)
		{
			if (isCut){inStr.insert(WordCount,1,ofBreak);WordCount++;}
			else
			{for (; inStr[WordCount] != ' ';){ WordCount++; }inStr.insert(WordCount, 1, ofBreak);}
		}
	}
}

void STR::hex2str(string& toStr)
{
	
}

string STR::chunk_spilt(string inStr,int inWidth,char inSymbol)
{
	string inReturn = inStr;
	wordwrap(inReturn, inWidth, inSymbol, true);
	return inReturn;
}

void STR::cchunk_spilt(string& inStr, int inWidth, char inSymbol)
{
	wordwrap(inStr, inWidth, inSymbol, true);
}

void STR::chop(string& inStr, string inList)
{
	EXSTR
	{
		for (string::iterator itor_in = inList.begin(); itor_in != inList.end(); itor_in++)
		{
			if (*itor == *itor_in)
			{
				inStr.erase(itor, itor + 1);
				if (itor != inStr.begin()) itor--;
			}
		}
	}
}

char STR::get_char(int inASCII)
{
	return (char)inASCII;
}

int STR::get_ascii(char inChar)
{
	return (int)inChar;
}